// const bodyParser = require('koa-bodyparser')
const koaBody = require('koa-body')
const cors = require('koa2-cors')
const static = require('koa-static-server')
const session = require('koa-session')
const path = require('path')
// const session = require('koa-generic-session')
// const redisStore = require('koa-redis')
const config = require('../config')

module.exports = app => {
  app.use(static({
    rootDir: path.resolve(__dirname, '../', 'resources/upload'),
    rootPath: '/upload'
  }))
  app.use(static({
    rootDir: path.resolve(__dirname, '../', 'resources/images'),
    rootPath: '/images'
  }))
  app.keys = [config.server.SIGN_KEY]
  // app.use(session({
  //   store: redisStore({
  //     // Options specified here
  //   })
  // }))
  // session
  app.keys = [config.server.SIGN_KEY] // 我理解为一个加密的钥匙，类似一个token
  const CONFIG = {
      key: 'koa:sess',   // cookie key (default is koa:sess)
      maxAge: 1000 * 10,  // cookie的过期时间 maxAge in ms (default is 1 days)
      overwrite: true,  // 是否可以overwrite    (默认default true)
      httpOnly: true, // cookie是否只有服务器端可以访问 httpOnly or not (default true)
      signed: true,   // 签名默认true
      rolling: false,  // 在每次请求时强行设置cookie，这将重置cookie过期时间（默认：false）
      renew: false,  //(boolean) renew session when session is nearly expired,
  }
  app.use(session(CONFIG, app))
  app.use(cors())
  // app.use(bodyParser())
  app.use(koaBody({
    multipart: true,
    formidable: {
      maxFileSize: 200 * 1024 * 1024	// 设置上传文件大小最大限制，默认2M
    }
  }))

}
